/**
 * 
 */
package br.com.ufrn.aipetro.ga.view.form;

import java.beans.PropertyChangeEvent;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import eu.schudt.javafx.controls.calendar.DatePicker;
import br.com.ufrn.aipetro.ga.controller.maintenance.MaintenanceController;
import br.com.ufrn.aipetro.ga.model.persist.hibernate.HibernateDaoFactory;
import br.com.ufrn.aipetro.ga.view.interfaces.View;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

/**
 * @author Andre
 * 
 */
public class ChartController implements View<MaintenanceController>, Initializable {

	@FXML private LineChart<String, Number> chtGraph;
	@FXML private CategoryAxis chtGraphCatAxis;
	@FXML private NumberAxis chtGraphNumAxis;
	@FXML private DatePicker tfdDateStart;
	@FXML private DatePicker tfdDateFinish;
	@FXML private GridPane gridPane;

	private static final int INSERT_MODE = 0;
	private static final int UPDATE_MODE = 1;

	private int editMode;
	private MaintenanceController controller;

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		
		tfdDateStart = new DatePicker(Locale.ENGLISH);
		tfdDateStart.setDateFormat(new SimpleDateFormat("dd-MM-yyyy"));
		tfdDateStart.getCalendarView().todayButtonTextProperty().set("Today");
		tfdDateStart.getCalendarView().setShowWeeks(false);
		tfdDateStart.getStylesheets().add("ch/makery/address/view/DatePicker.css");
		tfdDateFinish = new DatePicker(Locale.ENGLISH);
		tfdDateFinish.setDateFormat(new SimpleDateFormat("dd-MM-yyyy"));
		tfdDateFinish.getCalendarView().todayButtonTextProperty().set("Today");
		tfdDateFinish.getCalendarView().setShowWeeks(false);
		tfdDateFinish.getStylesheets().add("ch/makery/address/view/DatePicker.css");

		  // Add DatePicker to grid
		  gridPane.add(tfdDateStart, 1, 0);
		  gridPane.add(tfdDateFinish, 3, 0);

	}

	public ChartController() {
		editMode = INSERT_MODE;
		initView(new MaintenanceController(new HibernateDaoFactory()));
	}

	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void initView(MaintenanceController controller) {
		this.controller = controller;
		this.controller.registerView(this);

	}

	public void loadGraph() {

		XYChart.Series<String, Number> serie = new XYChart.Series<>();
		XYChart.Series<String, Number> serie2 = new XYChart.Series<>();
		serie.setName(controller.getModel().getIdTbDevice().toString());
		List<Date> data2 = controller.persistSearchByDevice(controller.getModel().getIdTbDevice());
		List<Integer> data3 = controller.persistSearchByDevice2(controller.getModel().getIdTbDevice());

		for (int i = 0; i < data2.size(); i++) {
			serie.getData().add(new XYChart.Data<String, Number>(data2.get(i).toString(), data3.get(i)));
		}

		chtGraph.getData().add(serie);
		//chtGraph.getData().add(serie2);


	}

	@FXML
	private void saveForm() {
		if (this.editMode == INSERT_MODE)
			insertController();
		else
			updateController();

	}

	private void insertController()  {
		//loadFormToMaintenance();
		controller.persistInsert();
		setUpdateMode();
	}

	private void updateController()  {
		//loadFormToMaintenance();
		controller.persistUpdate();
	}

	@FXML
	private void cancelForm() {

	}

	@Override
	public void modelChange() {
		setUpdateMode();
		loadGraph();
	}

	private void setUpdateMode() {
		this.editMode = UPDATE_MODE;
	}



}
